package com.totoro.utils;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TOTORO
 * @date 2021/7/12 15:47
 */
@UtilityClass
public class IKParticiple {

    /**
     *
     * @param context 需要分词的文本
     * @param accuracy 分词密度大小控制 true 大 false 小
     * @return
     */
    @SneakyThrows
    public static String[] participle(String context, boolean accuracy){
        StringReader stringReader=new StringReader(context);
        IKSegmenter ikSegmenter=new IKSegmenter(stringReader,accuracy);
        Lexeme lexeme = null;
        List<String> list = new ArrayList<>();
        while( (lexeme = ikSegmenter.next())!=null ){
            list.add(lexeme.getLexemeText());
        }
        list.stream().distinct().collect(Collectors.toList());
        String[] texts = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            texts[i] = list.get(i);
        }

        return texts;
    }
}
